<?php
require_once('./modules/feeds/feedItem.inc');
require_once('./modules/cache.inc');
require_once('./modules/util.inc');

class FeedRenderer extends Renderer
{
	private $oFeedItem;
	private $_categoryInfo;
	
	function pageLoad()
	{
		switch( $this->action )
		{
			case "":
			case "show":
			default:
				break;
		}
		
		$this->getFeedItem($_GET['feedId']);
		$this->oFeedItem->content = Util::cleanTags($this->oFeedItem->content);
		$this->oFeedItem->title = Util::cleanTags($this->oFeedItem->title);		
	}
	
	private function getFeedItem($feedId)
	{
		$feedItem;
		
		//name the content type, with paging
		$cacheId = "feed-id-".$_GET['feedId'];

		$cacheStatus = 'DEAD';
		
		$cache = new Cache();
		
		if( !$cache->ERROR )
			$cacheStatus = $cache->check_cache($cacheId);
		
		if($cacheStatus == 'HIT')
		{
			$arr = $cache->get($cacheId);
			$this->oFeedItem = $arr["feed"];
			$this->_categoryInfo = $arr["category"];
		}
		else
		{
			$this->oFeedItem = new FeedItem();
			
			if( !$this->oFeedItem->load($_GET['feedId']))
			{
				header("Location: index.php?pg=404");
				die();
			}

			//category info not mandatory - if the item is not related to a category, it will be presented by the source
			//so we cannot rule out the possibility of such page being served.
			$this->_categoryInfo = $this->getCategoryInfo();

			$arr = Array();
			
			$arr["feed"] = $this->oFeedItem;
			$arr["category"] = $this->_categoryInfo;
			
			$cache->set($cacheId,$arr);
		}
		
		$this->oFeedItem->promoteViewCount();
	}

	function renderContent()
	{
		global $G_HOME;
		global $content;
		
		$content .= 
		"<div>
			<h2>".$this->oFeedItem->title."</h2>
			<h4>Source: <a href='".Util::combinePath($G_HOME,"index.php?pg=feed_source&sourceId=".$this->oFeedItem->feedId."&itemId=0")."'>".$this->oFeedItem->feedName."</a></h4>
			<h5>".date('d/m/y H:i',$this->oFeedItem->entryDate)."</h5>
		</div>";

		$text = substr($this->oFeedItem->content,0,min(strlen($this->oFeedItem->content),2000));
		if( strlen($text) >3 && substr($text,strlen($text)-3,3) != '...')
			$text .= "...";

		$content .= "<div style='line-height:1.5em;'>".$text."</div><p/>
				<h3><a rel=\"nofollow\" target=\"_blank\" href='".$this->oFeedItem->link."'>read more</a></h3>";
		
		if(isset($this->_categoryInfo))
		{
			$content .= "<div class=\"boxHeadline\">most popular reads in this category</div>";
			$content .= FeedBox::get5ClickByCategory($this->_categoryInfo['id']);
		}
	}
	
	function setSEODetails()
	{
		global $title;
		global $description;
		
		$title = Util::cutStringFullWords($this->config->get('./siteName')." - ".$this->oFeedItem->title,65).".";
		$description = Util::cutStringFullWords($this->config->get('./siteName').' - '.$this->oFeedItem->title." ".$this->oFeedItem->content,255).".";
	}
	
	function getCategoryInfo()
	{
		$array = Feed::getTagCloud($this->oFeedItem->feedId);
		
		$categoryInfo;
		
		foreach($array as $row)
		{
			if(isset($row['tag_id']))
			{
				$categoryInfo = $row;
				break;
			}
		}
		
		return $categoryInfo;
	}
	
	function renderBreadcrumbs()
	{
		parent::renderBreadcrumbs();
		
		global $breadcrumbs;
		
		global $G_HOME;
		
		if( isset($this->_categoryInfo))
		{
			$parentName = $this->_categoryInfo['tag'];
			$feedUrl = Util::combinePath($G_HOME,"index.php?pg=feed_category&catId=".$this->_categoryInfo['id']."&itemId=0");
		}
		else
		{
			$parentName = $this->oFeedItem->feedName;
			$feedUrl = Util::combinePath($G_HOME,"index.php?pg=feed_source&sourceId=".$this->oFeedItem->feedId."&itemId=0");
		}

		$feedItemName = $this->oFeedItem->title;
		
		$breadcrumbs .= "&nbsp;&#187;&nbsp;<a href='$feedUrl'>$parentName</a>&nbsp;&#187;&nbsp;$feedItemName";
	}
}

$renderer = new FeedRenderer();

?>